package com.mytx.sso;

import com.alibaba.fastjson.JSONObject;
import com.mytx.common.config.MytxConfig;
import com.mytx.mapper.MytxConfigMapper;
import com.mytx.pojo.MytxConfigEntiy;
import com.mytx.sso.utils.RefreshConfigUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 项目启动的时候去数据库读取配置文件信息
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(MyCommandLineRunner.class);

    @Autowired
    private MytxConfigMapper mytxConfigMapper;

    @Override
    public void run(String... args) throws Exception {
        logger.info("mytx-user项目启动成功...");
        logger.info("去数据库读取配置文件信息...");
        List<MytxConfigEntiy> mytxConfigList = mytxConfigMapper.findByAll();
        refreshConfig(mytxConfigList);
    }

    @Async
    public void refreshConfig(List<MytxConfigEntiy> mytxConfigList){
        RefreshConfigUtils.refreshUserConfig(mytxConfigList);
    }
}
